<?php
declare (strict_types=1);

namespace app\organization\controller\core;

use app\common\controller\Frontend;
use app\common\enums\AccountTypeEnum;
use app\common\enums\ClassHourStatusEnum;
use app\common\model\banxi\core\CoreAccountClassHour as CoreAccountClassHourModel;
use app\common\model\banxi\core\CoreClassHourDetail as CoreClassHourDetailModel;
use app\common\service\organization\OrganizationLevelService;
use app\common\service\organization\OrganizationRewardService;
use app\common\service\student\AccountClassHourService;
use ba\Exception;
use think\db\exception\DbException;
use think\db\exception\PDOException;
use think\exception\ValidateException;

class CoreAccountClassHour extends Frontend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new CoreAccountClassHourModel();
    }


    /**
     * 查询-课时数据
     * @return void
     * @throws DbException
     */
    public function getAccountClassHour(): void
    {
        $organizationId = $this->request->param('organizationId');
        $info = $this->model->where([
            'accountId' => $organizationId,
            'accountType' => AccountTypeEnum::ORGAN
        ])->find();
        $this->ok('操作成功', removeNull($info));
    }

    /**
     * 查询-我的课时数据
     * @return void
     * @throws DbException
     */
    public function getMyAccountClassHour(): void
    {
        $organizationId = $this->auth->organizationId;
        $info = $this->model->where([
            'accountId' => $organizationId,
            'accountType' => AccountTypeEnum::ORGAN
        ])->find();
        $this->ok('操作成功', removeNull($info));
    }

    /**
     * 查询-是否付款成功
     * @return void
     * @throws DbException
     */
    public function getIsPaySuccess(): void
    {
        $orderId = $this->request->param('orderId');
        $coreClassHourDetailModel = new CoreClassHourDetailModel();
        $classHourDetail = $coreClassHourDetailModel->where(['id' => $orderId])->find();
        $flag = false;
        if ($classHourDetail['isLinePay'] && $classHourDetail['status'] == ClassHourStatusEnum::SUCCESS) {
            $flag = true;
        }

        $this->ok('ok', $flag);
    }

    /**
     * 取消支付
     * @return void
     * @throws DbException
     */
    public function cancelPay(): void
    {
        $id = $this->request->param('id');
        $coreClassHourDetailModel = new CoreClassHourDetailModel();
        $classHourDetail = $coreClassHourDetailModel->where(['id' => $id])->find();
        if ($classHourDetail['isLinePay'] && $classHourDetail['status'] == ClassHourStatusEnum::IN) {
            $coreClassHourDetailModel->update([
                'status' => ClassHourStatusEnum::CANCEL
            ], [
                'id' => $id,
                'status' => ClassHourStatusEnum::IN
            ]);
        }
        $this->ok('ok');
    }


    //充值机构
    public function rechargeOrgan()
    {
        $param = $this->request->param(['organizationId', 'classHourType', 'classHourNum', 'payAmount', 'rechargeType', 'payType', 'memo', 'password']);
        try {
            $user = $this->auth->getUser();
            $isError = verifyMd5Password($param['password'], $user['password'], $user['salt']);
            if (!$isError) {
                throw new ValidateException('密码错误');
            }
            $accountClassHourService = new AccountClassHourService();
            $res = $accountClassHourService->rechargeOrgan(true, $param);
            if (!$res) {
                throw new ValidateException($accountClassHourService->getError());
            }
            // //新校区陪跑服务奖励
            $organizationRewardService = new OrganizationRewardService();
            $organizationRewardService->accompanyServiceReward($param);
            //校区升级
            $organizationRewardService = new OrganizationLevelService();
            $organizationRewardService->campusUpgrade($param['organizationId']);
        } catch (Exception|PDOException|ValidateException $e) {
            $this->fail($e->getMessage());
        }
        $this->ok('操作成功');

    }

    /**
     * 充值学员
     */
    public function rechargeStudent()
    {
        $param = $this->request->param(['studentId', 'classHourNum', 'classHourType', 'payType', 'rechargeType', 'password']);
        try {
            $accountClassHourService = new AccountClassHourService();
            $res = $accountClassHourService->jgRechargeStudent($param);
            if (!$res) {
                throw new ValidateException($accountClassHourService->getError());
            }
            //合伙人升级
            $organizationLevelService = new OrganizationLevelService();
            $organizationLevelService->partnerUpgrade($param['studentId']);
        } catch (Exception|PDOException|ValidateException $e) {
            $this->fail($e->getMessage());
        }
        $this->ok('ok', $res);

    }

    //退费机构
    public function refundOrgan()
    {
        $param = $this->request->param(['organizationId', 'classHourType', 'classHourNum', 'memo', 'password']);
        try {
            $user = $this->auth->getUser();
            $isError = verifyMd5Password($param['password'], $user['password'], $user['salt']);
            if (!$isError) {
                throw new ValidateException('密码错误');
            }
            $accountClassHourService = new AccountClassHourService();
            $res = $accountClassHourService->refundOrgan(true, $param);
            if (!$res) {
                throw new ValidateException($accountClassHourService->getError());
            }
        } catch (Exception|PDOException|ValidateException $e) {
            $this->fail($e->getMessage());
        }
        $this->ok('操作成功');

    }


}
